package org.xfy.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;
import org.xfy.sys.domain.DictionaryDetail;
import org.xfy.sys.query.DictionaryDetailQuery;
import org.xfy.sys.service.IDictionaryDetailService;

import java.util.List;

@RestController
@RequestMapping("/dictionaryDetail")
@Api(value = "数据字典的详情API", description = "数据字典相关的详情CRUD功能")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @PostMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult save(@RequestBody DictionaryDetail dictionaryDetail) {
        if (dictionaryDetail.getId() == null) {//新增
            dictionaryDetailService.add(dictionaryDetail);
        } else {
            dictionaryDetailService.update(dictionaryDetail);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public AjaxResult del(@PathVariable("id") Long id) {
        dictionaryDetailService.del(id);
        return AjaxResult.me();
    }

    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public AjaxResult PatchDel(@RequestBody Long[] ids) {
        dictionaryDetailService.patchDel(ids);
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult findOne(@PathVariable("id") Long id) {
        DictionaryDetail dictionaryDetail = dictionaryDetailService.findOne(id);
        return AjaxResult.me().setResultObj(dictionaryDetail);
    }

    @GetMapping("/byTypeid/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult findDetailsByTypeid(@PathVariable("id") Long id) {
         List<DictionaryDetail> list=dictionaryDetailService.findDetailsByTypeid(id);
        return AjaxResult.me().setResultObj(list);
    }

    @GetMapping()
    @ApiOperation(value = "查询所有")
    public AjaxResult findAll() {
        List<DictionaryDetail> dictionaryDetails = dictionaryDetailService.findAll();
        return AjaxResult.me().setResultObj(dictionaryDetails);
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody DictionaryDetailQuery query) {
        PageList<DictionaryDetail> pageList = dictionaryDetailService.queryPage(query);
        return AjaxResult.me().setResultObj(pageList);
    }

}
